/*
 * jlb
 */
package com.jlb.item.service;

import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.ItemCategoryException;
import com.jlb.item.dao.ItemCategoryDAO;
import com.jlb.item.entity.ItemCategoryState;
import com.jlb.item.entity.ItemCategory;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("itemCategorySV")
@Slf4j
public class ItemCategorySVImpl extends BaseSVImpl<ItemCategory, Long> implements ItemCategorySV {

    @Autowired
    private ItemCategoryDAO itemCategoryDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return itemCategoryDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(ItemCategory entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象ItemCategory
     *
     * @param id * @param code 编码
     * @return ItemCategory
     */
    @Override
    public ItemCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return itemCategoryDAO.load(param);
    }

    /**
     * 加载一个对象ItemCategory 通过id
     *
     * @param id
     * @return ItemCategory
     */
    @Override
    public ItemCategory loadById(Long id) {
        if (id == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return itemCategoryDAO.loadById(id);
    }

    /**
     * 加载一个对象ItemCategory 通过code
     *
     * @param code 编码
     * @return ItemCategory
     */
    @Override
    public ItemCategory loadByCode(String code) {
        if (code == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return itemCategoryDAO.loadByCode(code);
    }


    /**
     * 根据主键id,oldStates 共同更新 ItemCategory 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ItemCategoryState newState, ItemCategoryState... oldStates) {
        if (id == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemCategoryDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键code,oldStates 共同更新 ItemCategory 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, ItemCategoryState newState, ItemCategoryState... oldStates) {
        if (code == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemCategoryDAO.updateStateByCode(code, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 ItemCategory 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ItemCategoryState state) {
        if (id == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemCategoryDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键code 更新 ItemCategory 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, ItemCategoryState state) {
        if (code == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        itemCategoryDAO.updateByCode(code, state, new Date());
    }


    /**
     * 删除对象ItemCategory
     *
     * @param id * @param code 编码
     * @return ItemCategory
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new ItemCategoryException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        itemCategoryDAO.delete(param);
    }


    /**
     * 查询ItemCategory分页
     *
     * @param itemCategory 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<ItemCategory>
     */
    @Override
    public List<ItemCategory> list(ItemCategory itemCategory, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (itemCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(itemCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return itemCategoryDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ItemCategory itemCategory) {
        Map<String, Object> map = null;
        if (itemCategory != null) {
            map = JSON.parseObject(JSON.toJSONString(itemCategory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return itemCategoryDAO.count(map);
    }

    /**
     * 查询ItemCategory分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ItemCategory>
     */
    @Override
    public List<ItemCategory> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return itemCategoryDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return itemCategoryDAO.count(param);
    }

}
